<?php
require_once ROOT_LIBRARY_UTILITY.DS. 'Globals.php';

class BaseModel
{
    protected  $_key = null;

    protected $_tableName = null;

    /**
     *
     * @var Zend_Db_Adapter_Abstract
     */
    protected $_db;

    protected function  __construct()
    {
        $this->_db = Globals::getDBConnection();
    }

    public function  _insert($obj)
    {
        try
        {
            $data = Globals::transferObjectToArray($obj);
            $this->_db->insert($this->_tableName, $data);
            $id = $this->_db->lastInsertId();

            return $id;

        }
        catch (Zend_Db_Exception $ex)
        {
            return $ex->getMessage();
        }
        catch(Exception $ex)
        {
            return $ex->getMessage();
        }
    }

    public function _update($obj)
    {
        try
        {
            $data = Globals::transferObjectToArray($obj);
            $key = $data[$this->_key];
            unset($data[$this->_key]);
            $this->_db->update($this->_tableName, $data, "$this->_key = $key");

            return $key;
        }
        catch (Zend_Db_Exception $ex)
        {
            return $ex->getMessage();
        }
        catch(Exception $ex)
        {
            return $ex->getMessage();
        }
    }

    public function _delete($value, $section = null)
    {
        try
        {
            if(null != $section)
                $this->_db->delete($this->_tableName, "$section = '$value'");
            else
                $this->_db->delete($this->_tableName, "$this->_key = '$value'");
        }
        catch (Zend_Db_Exception $ex)
        {
            return $ex->getMessage();
        }
        catch(Exception $ex)
        {
            return $ex->getMessage();
        }
    }

    public function _approve($id,$userId)
    {
        try
        {
            $obj = $this->_getByKey($id);

            if(!is_null($obj->article_types_id))
                $where = "article_types_id = $obj->article_types_id";
            else if (!is_null($obj->service_types_id))
                $where = "service_types_id = $obj->service_types_id";

            $sql = "UPDATE `$this->_tableName`
                    SET `approved_by` = '$userId'
                    AND `approved_date` = NOW()
                    WHERE `$this->_key` = '$id'
                    AND $where";
            
            $this->_db->query($sql);
                        
        }
        catch(Zend_Db_Exception $ex)
        {
            return $ex->getMessage();
        }
        catch(Exception $ex)
        {
            return $ex->getMessage();
        }
    }

    public function _getByKey($value)
    {
        try
        {
            $sql = "SELECT
                                    *
                            FROM
                                    `$this->_tableName`
                            WHERE
                                    `$this->_key` = '$value'";
            $data = $this->_db->fetchRow($sql, "", Zend_Db::FETCH_OBJ);

            return $data;
        }
        catch (Zend_Db_Exception $ex)
        {
            return $ex->getMessage();
        }
        catch(Exception $ex)
        {
            return $ex->getMessage();
        }
    }

    public function _getAll($where = null)
    {
        try
        {
            $sql = "SELECT
                                    *
                            FROM
                                    `$this->_tableName`
                            WHERE
                                    1";
            if ($where)
                $sql .= " AND $where";
            $data = $this->_db->fetchAll($sql, '', Zend_Db::FETCH_OBJ);

            return $data;
        }
        catch (Zend_Db_Exception $ex)
        {
            return $ex->getMessage();
        }
        catch(Exception $ex)
        {
            return $ex->getMessage();
        }
    }
}
?>
